<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2018-08-10
  Time: 17:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>员工管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/table/bootstrap-table.min.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/table/bootstrap-table.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/table/locale/bootstrap-table-zh-CN.min.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/sweetalert/sweetalert.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/sweetalert/sweetalert.min.js"></script>

    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/date/bootstrap-datetimepicker.min.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/date/Moment.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/date/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#table').bootstrapTable({
                url:'${pageContext.request.contextPath}/staff/query',
                columns:[
                    {
                        field:'staffId',
                        title:'编号'
                    }, {
                        field:'staffname',
                        title:'名字'
                    },{
                        field:'staffPhone',
                        title:'手机号'
                    },{
                        field:'staffSex',
                        title:'性别',
                        formatter: function (value, row, index) {
                            if (row['staffSex'] === 1) {
                                return '男';
                            }
                            if (row['staffSex'] === 2) {
                                return '女';
                            }
                            return value;
                        }
                    }, {
                        field:'staffAge',
                        title:'年龄'
                    },{
                        field:'staffAddress',
                        title:'员工住址'
                    }, {
                        field:'staffData',
                        title:'入职日期'
                    },{
                        field:'staffwages',
                        title:'工资'
                    },
                    {
                        field:'xx',title:'操作',
                        formatter : function(value, row, index) {
                            return "<a title='删除' href='javascript:del("
                                + row.staffId + ")'><span class='glyphicon glyphicon-trash'></span></a>| <a onclick='upd1("+row.staffId+")' class='glyphicon glyphicon-pencil'></a>";
                        }

                    }
                ],
                method:'post',
                contentType:"application/x-www-form-urlencoded",
                queryParamsType:'',
                queryParams:queryParams,
                height:360,
                pageList:[5,10,15],
                pageNumber:1,
                pageSize:5,
                pagination:true,
                sidePagination:'server',
            });
        })

        //获取当前的条件个页面页数即使更新值
        function queryParams(afds){
            var i={
                "pageSize":afds.pageSize,
                "pageNumber":afds.pageNumber,
                "id":$('#eqName').val(),
            };
            return i;
        }
        //查询
        function chaxun(){
            var opt=$('#table').bootstrapTable('getOptions');
            var eqname=$('#eqname').val();
            $.post("${pageContext.request.contextPath}/staff/query",{"pageSize":opt.pageSize,"pageNumber":opt.pageNumber,"hyname":eqname},function (data) {

                $("#table").bootstrapTable('load',data) ;
            })
        }


        function del(staffId) {
            $.post("${pageContext.request.contextPath}/staff/delete",{"staffId":staffId},function (releset) {
                if(releset != null){
                    $("#table").bootstrapTable('load',releset) ;
                    swal(
                        {
                            title:"删除成功",
                            type:"success",
                            timer: 1500,
                            showConfirmButton: false
                        }
                    )

                }else{
                    swal(
                        {
                            title:"删除失败",
                            type:"warning",
                            timer: 1500,
                            showConfirmButton: false

                        }
                    )
                }
            })
        }

        //添加
        function insert() {
            var sname=$('').val();
            var sphone=$('').val();
            var ssex=$('').val();
            var sage=$('').val();
            var saddress=$('').val();
            var swages=$('').val();
            $('#exampleModal').modal('show');

        }

        function tianjia() {
            var sname=$('#name').val();
            var sphone=$('#phone').val();
            var ssex=$('#sex').val();
            var sage=$('#age').val();
            var saddress=$('#address').val();
            var swages=$('#wages').val();
            $.post("${pageContext.request.contextPath}/staff/insert",{"staffname":sname,"staffPhone":sphone,"staffSex":ssex,"staffAge":sage,"staffAddress":saddress,"staffwages":swages,},function (releset) {
                if(releset != null){
                    $("#table").bootstrapTable('load',releset) ;
                    $('#exampleModal').modal('hide');
                    swal(
                        {
                            title:"添加成功",
                            type:"success",
                            timer: 1500,
                            showConfirmButton: false
                        }
                    )
                }else{
                    swal(
                        {
                            title:"添加失败",
                            type:"warning",
                            timer: 1500,
                            showConfirmButton: false
                        }
                    )
                }
            })
        }

        function upd1(staffId){
            $("#exampleModal2").modal("show");
            // $('#id').val(id);
            // var id=$('id').val();
            alert(staffId);
            $.post('${pageContext.request.contextPath}/staff/cha',{'staffId':staffId},function(data){
                $("#upid2").val(staffId);
                $("#name2").val(data.staffname);
                $("#phone2").val(data.staffPhone);
                $("#sex2").val(data.staffSex);
                $("#age2").val(data.staffAge);
                $("#address2").val(data.staffAddress);
                $("#wages2").val(data.staffwages);
                // $('input[name="xgsex"]:checked').val(data.coachSex);
                // $("input[name=xgsex][value="+data.coachSex+"").attr("checked",true);

            }) ;

        }

        function upd() {
            var sid=$('#upid2').val();
            var sname=$('#name2').val();
            var sphone=$('#phone2').val();
            var ssex =$('#sex2').val();
            var sage=$('#age2').val();
            var saddress=$('#address2').val();
            var swages=$('#wages2').val();
            $.post("${pageContext.request.contextPath}/staff/update",{"staffId":sid,"staffname":sname,"staffPhone":sphone,"staffSex":ssex,"staffAge":sage,"staffAddress":saddress,"staffwages":swages,},function (releset) {
                $("#table").bootstrapTable('load',releset) ;
                if(releset != null){
                    $("#dg").bootstrapTable('load',releset) ;
                    $('#exampleModal2').modal('hide');
                    swal(
                        {
                            title:"修改成功",
                            type:"success",
                            timer: 1500,
                            showConfirmButton: false
                        }
                    )
                    chaxun();
                }else{
                    swal(
                        {
                            title:"修改失败",
                            type:"warning",
                            timer: 1500,
                            showConfirmButton: false
                        }
                    )
                }
            })
        }

    </script>
</head>
<body>

<%--    //查询--%>
<div class="panel panel-default">
    <div class="panel-body">
        <form class="form-inline">
            <div  class="input-group input-daterange">
                <label for="eqname" class="control-label">员工名称:</label>
                <input id="eqname" type="text" class="form-control">
            </div>
            <button onclick="chaxun()" type="button" class="btn btn-default" style="margin-top: 20px" >查询</button>
            <button type="button" class="btn btn-default" style="float: right; margin-top: 20px" data-toggle="modal" onclick="insert()"><span class="glyphicon glyphicon-plus"></span>添加员工信息</button>
        </form>
    </div>

</div>

    <!--表格-->
    <table id="table"></table>

<%--添加--%>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="updateModalLabel">员工添加</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" id="upid">
                    <div class="form-group">
                        <label class="control-label">员工名字:</label>
                        <input type="text" class="form-control" id="name">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">手机号:</label>
                        <input type="text" class="form-control" id="phone">
                    </div>
                    <div class="form-group">
                        <label class="control-label">性别:</label>
                        <select class="form-control" id="sex">
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label  class="control-label">年龄:</label>
                        <input type="text" class="form-control" id="age">
                    </div><div class="form-group">
                    <label class="control-label">住址:</label>
                    <input type="text" class="form-control" id="address">
                </div>
                    <div class="form-group">
                        <label  class="control-label">工资:</label>
                        <input type="text" class="form-control" id="wages">
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="tianjia()">添加</button>
            </div>
        </div>
    </div>
</div>

<%--修改--%>
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="updateModalLabel2">信息修改</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" id="upid2">
                    <div class="form-group">
                        <label class="control-label">员工名字:</label>
                        <input type="text" class="form-control" id="name2">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">手机号:</label>
                        <input type="text" class="form-control" id="phone2">
                    </div>
                    <div class="form-group">
                        <label class="control-label">性别:</label>
                        <select class="form-control" id="sex2">
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label  class="control-label">年龄:</label>
                        <input type="text" class="form-control" id="age2">
                    </div><div class="form-group">
                    <label class="control-label">住址:</label>
                    <input type="text" class="form-control" id="address2">
                </div>
                    <div class="form-group">
                        <label  class="control-label">工资:</label>
                        <input type="text" class="form-control" id="wages2">
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="upd()">添加</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
